
//
//  ShoppingViewController.m
//  瀑布流
//
//  Created by mac on 2017/3/16.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "ShoppingViewController.h"
#import "WaterFallView.h"
#import "ShoppingCell.h"
#import "ShoppingModel.h"

//首先在ViewController.m里面加入常量定义 即π/180
#define CRotate  (3.1415926/180.0)


@interface ShoppingViewController ()<WaterFallViewDelegate, WaterFallViewDataSource>

@property (nonatomic, strong) NSMutableArray *shops;
@property (nonatomic, strong) WaterFallView *waterFall;

@end

@implementation ShoppingViewController

- (NSMutableArray *)shops{
    if (_shops == nil) {
        self.shops = [NSMutableArray array];
    }
    return _shops;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];

    //1.创建数据源
    [self createData];
    
    //2.创建瀑布流
    [self createWaterFall];
    
    //3.上拉刷新、下拉加载更多
    [self.waterFall addHeaderWithTarget:self action:@selector(loadNewData)];
    [self.waterFall addFooterWithTarget:self action:@selector(loadMoreData)];
    
    
}

// 支持设备自动旋转
- (BOOL)shouldAutorotate
{
    return YES;
}

/**
 *  设置特殊的界面支持的方向,这里特殊界面只支持Home在右侧的情况
 */
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortrait;
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
    
    [self.waterFall reloadData];
}

- (void)loadNewData{
    //使用单利，加载1.plist中的数据，只能加载一次
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        //加载1.plist
        NSArray *newShops = [ShoppingModel mj_objectArrayWithFilename:@"1.plist"];
        [self.shops insertObjects:newShops atIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, newShops.count)]];
    });
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        //刷新瀑布流
        [self.waterFall reloadData];
        
        //停止刷新
        [self.waterFall headerEndRefreshing];
    });

}

- (void)loadMoreData{

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        //加载3.plist中的数据
        NSArray *moreShops = [ShoppingModel mj_objectArrayWithFilename:@"3.plist"];
        
        [self.shops addObjectsFromArray:moreShops];
    });
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        //刷新瀑布流
        [self.waterFall reloadData];
        
        //停止刷新
        [self.waterFall footerEndRefreshing];
    });
    
}


//创建数据源
- (void)createData{
   //从本地 plist中加载数据
    //使用 "MJExtension.h" 实现，字典转模型
    NSArray *newShops = [ShoppingModel mj_objectArrayWithFilename:@"2.plist"];
    [self.shops addObjectsFromArray:newShops];

}

//创建瀑布流
- (void)createWaterFall{
    WaterFallView *waterFall = [[WaterFallView alloc] init];
    waterFall.backgroundColor = [UIColor redColor];
    waterFall.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
//    waterFall.frame = CGRectMake(0, 64, self.view.width, self.view.height - 64);
//    waterFall.frame = self.view.bounds;
    waterFall.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    
//    CGFloat max = MAX(SCREEN_HEIGHT, SCREEN_WIDTH);
//    CGFloat min = MIN(SCREEN_HEIGHT, SCREEN_WIDTH);
//    _player.view.frame = CGRectMake(0, 0, min, max);
    
    waterFall.delegate = self;
    waterFall.dataSource = self;
    [self.view addSubview:waterFall];
    
    self.waterFall = waterFall;
}

#pragma mark ----- 代理方法
//数据源
- (NSUInteger)numberOfCellsInWaterfall:(WaterFallView *)waterFallView{
    
    return self.shops.count;
}
//多少列
- (NSUInteger)numberOfColumnsInWaterfall:(WaterFallView *)waterFallView{
    //如果是横屏：展示5列，如果是竖屏：展示3列
    if (UIInterfaceOrientationIsPortrait(self.interfaceOrientation)) {
        //竖屏
        return 3;
    }else{
        return 5;
    }
    
}
//cell
- (WaterFallViewCell *)waterFallView:(WaterFallView *)waterFallView cellAtIndex:(NSUInteger)index{
    ShoppingCell *cell = [ShoppingCell cellWithWaterFallView:waterFallView];
    cell.shop = self.shops[index];

    return cell;
}
//cell 高
- (CGFloat)waterFallView:(WaterFallView *)waterFallView heightAtIndex:(NSUInteger)index{
//    //当前列号：0%3=0 -》商：0，余：0；1%3=1-》商0，余1；2%3=2，商0，余2
//    NSUInteger result = index % 3;
//    
//    if (result == 0) {
//        //第0列
//        return 70;
//    }else if (result == 1){
//        //第1列
//        return 100;
//        
//    }else if (result == 2){
//        //第2列
//        return 80;
//    }
//    return 80;
    
    ShoppingModel *shop = self.shops[index];
    //根据cell的宽度和图片的宽高比 计算出 cell的高度
    return waterFallView.cellWidth * shop.h / shop.w;
}

- (CGFloat)waterFallView:(WaterFallView *)waterFallView marginType:(WaterFallMarginType)marginType{
    
    switch (marginType) {
        case WaterFallMarginTypeTop:
        case WaterFallMarginTypeLeft:
        case WaterFallMarginTypeRight:
        case WaterFallMarginTypeBottom:
            
            return 10;
            
        default: return 5;
    }
}

- (void)waterFallView:(WaterFallView *)waterFallView didSelectAtIndex:(NSUInteger)index{
    
    NSLog(@"点击了cell--- %ld", index);
    
}




@end
